package com.ziyun.erp.web.modules.order.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.order.vo.OrderRandomConfigVo;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.SysUserEntity;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.erp.modules.order.entity.OrderRandomConfigEntity;
import com.ziyun.erp.modules.order.service.OrderRandomConfigService;

import io.renren.common.utils.DateUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 订单随机立减配置表
 *
 * @author 
 * @email 
 * @date 2018-09-04 11:38:50
 */
@RestController
@RequestMapping("order/orderrandomconfig")
public class OrderRandomConfigController {
    @Autowired
    private OrderRandomConfigService orderRandomConfigService;

    /**
     * 列表
     */
    @RequestMapping("/randomConfigList")
    @RequiresPermissions("order:orderrandomconfig:list")
    public R randomConfigList(@RequestBody  Map<String, Object> params){
        List<OrderRandomConfigEntity> randomConfigList = orderRandomConfigService.randomConfigList(params);

        return R.ok().put("randomConfigList", randomConfigList);
    }

    /**
     * 批量更新
     */
    @RequestMapping("/batchUpdate")
    @RequiresPermissions("order:orderrandomconfig:save")
    public R batchUpdate(@RequestBody OrderRandomConfigVo orderRandomConfigVo){
        //获取创建人
        SysUserEntity user=(SysUserEntity)SecurityUtils.getSubject().getPrincipal();
        orderRandomConfigVo.setUpdateName(user.getUsername());
        orderRandomConfigVo.setUpdateData(new Date());
        orderRandomConfigService.batchUpdate(orderRandomConfigVo);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("order:orderrandomconfig:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderRandomConfigService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{discountId}")
    @RequiresPermissions("order:orderrandomconfig:info")
    public R info(@PathVariable("discountId") Integer discountId){
        OrderRandomConfigEntity orderRandomConfig = orderRandomConfigService.selectById(discountId);

        return R.ok().put("orderRandomConfig", orderRandomConfig);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("order:orderrandomconfig:save")
    public R save(@RequestBody OrderRandomConfigEntity orderRandomConfig){
    	//获取创建人
    	SysUserEntity user=(SysUserEntity)SecurityUtils.getSubject().getPrincipal();
    	orderRandomConfig.setCreateName(user.getUsername());
        orderRandomConfigService.insert(orderRandomConfig);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("order:orderrandomconfig:update")
    public R update(@RequestBody OrderRandomConfigEntity orderRandomConfig){
        ValidatorUtils.validateEntity(orderRandomConfig);
        //先将日期转换成字符串
        String stringDate = DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN);
        //再转换成日期存回去
        orderRandomConfig.setUpdateTime(DateUtils.stringToDate(stringDate, DateUtils.DATE_TIME_PATTERN));
        orderRandomConfig.setUpdateName(UserUtils.getCurentUser().getUsername());
        orderRandomConfigService.updateAllColumnById(orderRandomConfig);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("order:orderrandomconfig:delete")
    public R delete(@RequestBody Integer[] discountIds){
        //orderRandomConfigService.deleteBatchIds(Arrays.asList(discountIds));
    	orderRandomConfigService.deleteUpdateBatchByIds(discountIds);
        return R.ok();
    }

}
